// validate.js
export function validateRequired(value) {
	if (Array.isArray(value)) {
		
		return value.length === 0;
	} else if (typeof value === 'string') {
		
		return !value || value.trim() === '';
	} else if (value === null || value === undefined) {
		
		return true;
	} else if (typeof value === 'number') {
	
		return false;
	}

	return false;
}

export const validate = (form, data) => {
	
	return new Promise((res, rej) => {
		try {
			form.forEach(v => {
				if (v.require || v.require === undefined) {
					if (validateRequired(data[v.key])) {
						uni.showToast({duration:3000,
							title: v.message,
							icon: 'none'
						})
						throw new Error('Break loop'); // 模拟跳出循环
					}
				}
			});
			res()
		} catch (e) {
			if (e.message !== 'Break loop') {
				rej()
				throw e; // 重新抛出其他异常
			}
		}
	})
	
}

const validatePlugin = {
	install(Vue) {
		Vue.prototype.$mValidate = validate
	}
};


export default validatePlugin;